import vibrator from '@system.vibrator'
import barcode from '@system.barcode'
import brightness from '@system.brightness'
import volume from '@system.volume'
import prompt from '@system.prompt';
import router from '@system.router';

export const DOG_ACTION_OBJECT_ARRAY = [
    { name: '犬吠', act: 'Corgi_Bark' },
    { name: '犬咬', act: 'Corgi_Bite' },
    { name: '犬受伤', act: 'Corgi_Damage' },
    { name: '犬跳', act: 'Corgi_Fall' },
    { name: '静止', act: 'Corgi_Idle' },
    { name: '集中注意力', act: 'Corgi_Idle_Focused' },
    { name: '高兴', act: 'Corgi_Idle_Happy' },
    { name: '空中翻滚', act: 'Corgi_Air_Flip' },
    { name: '握手', act: 'Corgi_Give_Paw' },
    { name: '听到动静', act: 'Corgi_Hear_Something' },
    { name: '趴着休息', act: 'Corgi_Idle_Lay_Down' },
    { name: '坐着休息', act: 'Corgi_Idle_Sit' },
    { name: '睡觉', act: 'Corgi_Idle_Sleep' },
    { name: '跳起', act: 'Corgi_Jump_Up' },
    { name: '落地', act: 'Corgi_Land' },
    { name: '跑步', act: 'Corgi_Run' },
    { name: '原地跑步', act: 'Corgi_Run_In_Place' },
    { name: '左转跑步', act: 'Corgi_Run_Turn_Left' },
    { name: '右转跑步', act: 'Corgi_Run_Turn_Right' },
    { name: '抓痒', act: 'Corgi_Scratch' },
    { name: '摇晃身体', act: 'Corgi_Shake' },
    { name: '嗅闻', act: 'Corgi_Sniff' },
    { name: '歪头', act: 'Corgi_Tilt_Head' },
    { name: '行走', act: 'Corgi_Walk' },
    { name: '倒退走', act: 'Corgi_Walk_Backwards' },
    { name: '原地倒退走', act: 'Corgi_Walk_Backwards_In_Place' },
    { name: '原地走', act: 'Corgi_Walk_In_Place' },
    { name: '左转走', act: 'Corgi_Walk_Turn_Left' },
    { name: '右转走', act: 'Corgi_Walk_Turn_Right' }
]

export const SYSTEM_ACTION_OBJECT_ARRAY = [
  { name: '手机震动', act: 'viberate' },
  { name: '打开手机二维码', act: 'qrcode' },
  { name: '手机屏幕更亮', act: 'screen_bright'},
  { name: '手机屏幕更暗', act: 'screen_dark' },
  { name: '手机音量更高', act: 'volume_high' },
  { name: '手机音量更低', act: 'volume_low' },
  { name: '拍照识别物体', act: 'camera' },
  { name: '讲故事', act: 'story' },
]

export const DOG_ACTION_NAME_ARRY = DOG_ACTION_OBJECT_ARRAY.map(item => item.name)

export const DOG_ACTION_VALUE_ARRY = DOG_ACTION_OBJECT_ARRAY.map(item => item.act)

export const SYSTEM_ACTION_NAME_ARRY = SYSTEM_ACTION_OBJECT_ARRAY.map(item => item.name)
  

export function queryAction(name) {
    for (let i = 0; i < DOG_ACTION_OBJECT_ARRAY.length; i++) {
      if (DOG_ACTION_OBJECT_ARRAY[i].name === name) {
        return DOG_ACTION_OBJECT_ARRAY[i].act;
      }
    }
    return '';
}

export function querySystemAction(name) {
  for (let i = 0; i < SYSTEM_ACTION_OBJECT_ARRAY.length; i++) {
    if (SYSTEM_ACTION_OBJECT_ARRAY[i].name === name) {
      return SYSTEM_ACTION_OBJECT_ARRAY[i].act;
    }
  }
  return '';
}


export function callSystemFunction(actionValue) {
  if (actionValue === 'viberate') {
    vibrator.vibrate({
      mode: 'long'
    })
  } else if (actionValue === 'qrcode') {
    barcode.scan({
      success: function(data) {
        console.log(`handling success: ${data.result}`)
      },
      fail: function(data, code) {
        console.log(`handling fail, code = ${code}, errorMsg=${data}`)
      }
    })
  } else if (actionValue === 'screen_bright') {
    brightness.getValue({
      success: function(data) {
        console.log(`handling success, value = ${data.value}`)
        let setValue =  data.value + 50
        if (setValue > 254) {
          setValue = 254
        }
        brightness.setValue({value: setValue})
        prompt.showToast({
          message: '亮度设置成功',
          duration: 0
        });
      },
      fail: function(data, code) {
        console.log(`handling fail, code = ${code}`)
      }
    })
  } else if (actionValue === 'screen_dark') {
    brightness.getValue({
      success: function(data) {
        console.log(`handling success, value = ${data.value}`)
        let setValue =  data.value - 50
        if (setValue < 1) {
          setValue = 1
        }
        brightness.setValue({value: setValue})
        prompt.showToast({
          message: '亮度设置成功',
          duration: 0
        });
      },
      fail: function(data, code) {
        console.log(`handling fail, code = ${code}`)
      }
    })
  } else if (actionValue === 'volume_high') {
    volume.getMediaValue({
      success: function(data) {
        console.log(`handling success: ${data.value}`)
        let setValue =  data.value + 0.15
        if (setValue > 1) {
          setValue = 1
        }
        volume.setMediaValue({value: setValue})
        prompt.showToast({
          message: '音量设置成功',
          duration: 0
        });
      },
      fail: function(data, code) {
        console.log(`handling fail, code = ${code}`)
      }
    })

  } else if (actionValue === 'volume_low') {
    volume.getMediaValue({
      success: function(data) {
        console.log(`handling success: ${data.value}`)
        let setValue =  data.value - 0.15
        if (setValue < 0) {
          setValue = 0
        }
        volume.setMediaValue({value: setValue})
        prompt.showToast({
          message: '音量设置成功',
          duration: 0
        });
      },
      fail: function(data, code) {
        console.log(`handling fail, code = ${code}`)
      }
    })
  } else if (actionValue === 'camera') {
    router.push({
      uri: '/pages/camera'
    })
  } else if (actionValue === 'story') {
    router.push({
      uri: '/pages/story'
    })
  }
  
}

  